<?php
class Util_MultiPage {
    public static function multiPage($uri_prefix,$uri_params,$kw,$total_rows,$page_size,$show_num,$simple = false) {
        $uri_prefix .= (substr($uri_prefix, -1) == '?') ? '' : '?';//检测是否带？号
        $kw = urlencode($kw);
        $show_num = $show_num - 1;
        if($total_rows > $page_size) {
            $pagenum = ceil($total_rows/$page_size);
        } else {
            $pagenum = 1;
        }
        if(isset($uri_params[Const_BaseInfo::PARAM_N_PAGE])) {
            $page = $uri_params[Const_BaseInfo::PARAM_N_PAGE];
        } else {
            $page = 1;
        }
        $startnum = $page - floor($show_num/2);
        if($startnum < 1) {
            $startnum = 1;
        }
        $endnum = $startnum + $show_num;
        if($endnum > $pagenum) {
            $endnum = $pagenum;
            $startnum = $endnum - $show_num;
        }
        if($startnum < 1) {
            $startnum = 1;
        }
        $list = array();
        for($i=$startnum;$i<=$endnum;$i++) {
            $uri_params[Const_BaseInfo::PARAM_N_PAGE] = $i;
           // if ($uri_params[Const_BaseInfo::PARAM_N_PAGE] <= 1) {
           //     unset($uri_params[Const_BaseInfo::PARAM_N_PAGE]);
            //    $row['link'] = substr($uri_prefix, 0, -1).$kw.http_build_query($uri_params);
           // }else{
                $row['link'] = $uri_prefix.$kw.http_build_query($uri_params);
            //}

            $row['page'] = $i;
            if($row['page'] == $page) {
                $row['selected'] = 1;
            } else {
                $row['selected'] = 0;
            }
            $list[] = $row;
        }
        $multipage = array("list"=>$list);
        if(!$simple) {
            if($startnum>1) {
                $multipage['firstpage'] = self::getPage(1,$uri_params,$uri_prefix,$kw);
            } else {
                $multipage['firstpage'] = null;
            }
        }
        if($page > 1) {
            $multipage['prexpage'] = self::getPage($page - 1,$uri_params,$uri_prefix,$kw);
        } else {
            $multipage['prexpage'] = null;
        }
        if($page < $pagenum) {
            $multipage['nextpage'] = self::getPage($page + 1,$uri_params,$uri_prefix,$kw);
        } else {
            $multipage['nextpage'] = null;
        }
        if(!$simple) {
            if($endnum < $pagenum) {
                $multipage['endpage'] = self::getPage($pagenum,$uri_params,$uri_prefix,$kw);
            } else {
                $multipage['endpage'] = null;
            }
        }
        if(!$simple) {
            $multipage['totalrow'] = $total_rows;
            $multipage['totalpage'] = $pagenum;
            $multipage['page'] = $page;
        }
        unset($uri_params[Const_BaseInfo::PARAM_N_PAGE]);
        if(!$simple) {
            $uri_params[Const_BaseInfo::PARAM_N_PAGE] = "";
            $multipage['jumplink'] = $uri_prefix.$kw.http_build_query($uri_params);
        }
        return $multipage;
    }

    public static function getPage($page , $uri_params ,$uri_prefix , $kw) {
        $firstpage = array();
        $firstpage['page'] = $page ;
        $uri_params[Const_BaseInfo::PARAM_N_PAGE] = $page;
        /*if ($uri_params[Const_BaseInfo::PARAM_N_PAGE] <= 1) {
            unset($uri_params[Const_BaseInfo::PARAM_N_PAGE]);
            $uri_prefix = substr($uri_prefix, 0, -1);
        }*/
        $firstpage['link'] = $uri_prefix.$kw.http_build_query($uri_params);
        return $firstpage;
    }

    /*public static function getNextPage($page , $uri_params, $uri_prefix , $kw) {
       // $nextpage = array();
        return $nextpage;
    }*/
}